'''
Created on 2011-08-24

@author: michael
'''
import unittest
import time
import datetime

from receipt_saver.pages import receipt
from receipt_saver import datamodels
from receipt_saver import util

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import testbed

class Test(unittest.TestCase):
    
    def setUp(self):
        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        #set appropriate environmental variable
        self.testbed.setup_env(app_id="receiptsaver")
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Next, declare which service stubs you want to use.
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_user_stub()

    def tearDown(self):
        self.testbed.deactivate()
        
    """tests begin here"""

    def test_render(self):
        correct_html = 'string'
    
    def processReceipts(self):
        pass
    
    def numberReceipts(self):
        pass
    def test_User(self):
        datamodels.User().put()
        self.assertEqual(1, len(datamodels.User.all().fetch(2)))
    
    def test_Label(self):
        datamodels.Label().put()
        datamodels.Label().put()
        self.assertEqual(2, len(datamodels.Label.all().fetch(2)))
    
    def test_Label3(self):
        datamodels.Label(tag = 'yum').put()
        query = db.GqlQuery("SELECT * FROM Label WHERE tag = :1", 'yum')
        result = query.get()
        self.assertEqual(result.tag, 'yum')
    
    def test_Receipt(self):
        datamodels.Receipt().put()
        self.assertEqual(1, len(datamodels.Receipt.all().fetch(2)))
    
    def test_AddReceipt(self):
        #create a receipt for a user account
        curr_user = datamodels.User(key_name='root')
        
        datamodels.Label(tag = 'fooooooood',
              parent = curr_user.key()).put()
        datamodels.Label(tag = 'drinks',
              parent = curr_user.key()).put()
        q_l = db.GqlQuery("SELECT * FROM Label")
        r = q_l.fetch(2)
        r.sort()
                       
        new_receipt = datamodels.Receipt(dollars = 100,
                              cents = 0,
                              date = util.current_date(),
                              user = curr_user,
                              parent = curr_user.key())
        new_receipt.labels.append(r[0].key())
        new_receipt.labels.append(r[1].key())
        new_receipt.put()
        
        #grab receipt
        q = db.GqlQuery("SELECT * FROM Receipt WHERE ANCESTOR IS :1", curr_user.key())
        #q = Receipt.all().ancestor(curr_user.key()) #same thing as above
        r = q.fetch(2)
        
        
        self.assertEqual(r[0].dollars, 100)
        self.assertEqual(r[0].cents, 0)
        self.assertEqual(r[0].date, util.current_date())
        
        
        l = [i for i in curr_user.user_receipts]
        l = l[0]
        self.assertEqual(l.dollars, 100)
        self.assertEqual(len(r[0].labels), 2)
        
    
    def test_AddLabel(self):
        pass
    
    def test_QueryUser(self):
        #Query by User
        pass
    
    def test_QueryLabel(self):
        #Query by label
        curr_user = datamodels.User(key_name='root')
        datamodels.Label(tag = 'fooooooood',
              parent = curr_user.key(),
              key_name = "label1").put()
        datamodels.Label(tag = 'drinks',
              parent = curr_user.key(),
              key_name = "label2").put()
        q_l = db.GqlQuery("SELECT * FROM Label")
        r = q_l.fetch(2)
        r.sort()
         
        new_receipt = datamodels.Receipt(dollars = 100,
                              cents = 0,
                              date = util.current_date(),
                              user = curr_user,
                              parent = curr_user.key())
        
        new_receipt.labels.append(r[0].key())
        new_receipt.labels.append(r[1].key())
        new_receipt.put()
        pass
    
    def test_QueryReceipt(self):
        #Query by receipt

        pass
    
    def test_QueryMultiReceipts(self):
        pass

    def test_QueryReceipt_LabelLabel(self):
        pass
    
    def test_QueryReceipt_LabelDate(self):
        pass
    
    def test_func_current_date(self):
        t = time.localtime()
        date = datetime.date(t[0], t[1], t[2])
        
        self.assertEqual(date, util.current_date())
    
    def test_func_make_account(self):
        """need to get doc on testing with users package"""
        user = users.get_current_user()
        key = util.make_account('toxicseaweed@gmail.com', '1234')
        q = datamodels.User.all()
        r = q.get()
        #self.assertNotEqual(key, None) CANNOT TEST THAT KEY EXISTS!!!
        self.assertEqual('toxicseaweed@gmail.com', r.user_email)
        self.assertEqual('1234', r.user_id)
        
    def test_func_query_receipts_labels_exact1(self):
        u_key = 'root'
        label_keys = ['1']
        query = util.query_receipts_labels_exact(u_key, label_keys)
        
        compare_query="SELECT * FROM Receipt WHERE ANCESTOR IS root AND labels = 1"
        self.assertEqual(compare_query, query)
    
    def test_func_query_receipts_labels_exact2(self):
        u_key = 'root'
        label_keys = ['1', '2']
        query = util.query_receipts_labels_exact(u_key, label_keys)
        compare_query="SELECT * FROM Receipt WHERE ANCESTOR IS %s AND labels = %s AND labels = %s" %(u_key, label_keys[0], label_keys[1])
        self.assertEqual(compare_query, query)
        
    """Unable to get this to work, still working creating an AND_func
    def test_func_query_receipts_labels_exact3(self):
        u_key = 'root'
        user = User(key_name = u_key)
        lbl1 = Label(parent = user.key())
        lbl1.put()
        l1_k = lbl1.key()
        
        lbl2 = Label(parent = user.key())
        lbl2.put()
        l2_k = lbl2.key()
        
        receipt = Receipt(parent=user)
        receipt.labels.append(lbl1.key())
        receipt.put()
        
        #query_s = util.query_receipts_labels_exact(u_key, ['1'])
        query_s = "SELECT * FROM Receipt WHERE ANCESTOR IS :1"
        
        query = db.GqlQuery(query_s, u_key)
        result = query.fetch(2)
        
        self.assertNotEqual(None, result)
        self.assertEqual(len(result), 2)"""
        
    def test_func_exact_search_receipts1(self):
        pass
        
    def test_func_frmt_date(self):
        pass
    
    def test_func_all_labels(self):
        new_user = datamodels.User(user_email = 'haha',
                        user_id = '1234')
        new_user.put()
        l1 = datamodels.Label(tag='fun', user = new_user)
        l1.put()
        
        l2 = datamodels.Label(tag='out', user = new_user)
        l2.put()
        
        label_stf = util.all_labels('haha', '1234')
        label_names = label_stf[0]
        label_keys = label_stf[1]
        
        self.assertEqual(len(label_names), 2)
        self.assertEqual(len(label_keys), 2)
        
    def test_func_get_accnt(self):
        accnt = datamodels.User(user_email = 'michael',
                    user_id = '1234')
        accnt.put()
        
        saved_accnt = util.get_accnt('michael', '1234')
        
        self.assertNotEqual(saved_accnt, None)
        self.assertEqual(saved_accnt.user_email, accnt.user_email)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()